package com.miniportal.base.controller;


import cn.hutool.db.sql.Query;
import cn.hutool.db.sql.Wrapper;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.miniportal.base.constant.BaseEnum;
import com.miniportal.base.constant.impl.ParkEnum;
import com.miniportal.base.convert.ContactPeopleConvert;
import com.miniportal.base.convert.UserBaseInfoConvert;
import com.miniportal.base.pojo.CommonResponse;
import com.miniportal.base.pojo.dto.ContactPeopleDto;
import com.miniportal.base.pojo.dto.UserBaseInfoDto;
import com.miniportal.base.pojo.entity.ContactPeople;
import com.miniportal.base.pojo.entity.UserBaseInfo;
import com.miniportal.base.pojo.query.UserBaseInfoQuery;
import com.miniportal.base.service.IContactPeopleService;
import com.miniportal.base.service.IUserBaseInfoService;
import com.miniportal.base.utils.GeneratorCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户基础信息 前端控制器
 * </p>
 *
 * @author zhouje
 * @since 2022-05-30
 */
@RestController
@RequestMapping("userBase")
public class UserBaseInfoController {

    @Autowired
    private IUserBaseInfoService iUserBaseInfoService;
    @Autowired
    private IContactPeopleService iContactPeopleService;

    /**
     * 查询所有用户
     * @return
     */
    @GetMapping("queryUsers")
    private PageInfo<UserBaseInfo> queryUsers(UserBaseInfoQuery request) {
        return iUserBaseInfoService.queryUsers(request);
    }

    /**
     * 新建客户
     * 客户联系人重复不做插入
     * 客户重复提示已存在
     * @param request
     * @return
     */
    @PostMapping("addOne")
    private CommonResponse add(@Validated UserBaseInfoDto request) {
        //客户联系人去重
        ContactPeopleDto peopleDto = new ContactPeopleDto();
        peopleDto.setContactName(request.getContactPeople());
        peopleDto.setContactPhone(request.getContactPeoplePhone());
        QueryWrapper<ContactPeople> query = new QueryWrapper<ContactPeople>()
                .eq("contact_name", peopleDto.getContactName())
                .eq("contact_phone", peopleDto.getContactPhone());
        List<ContactPeople> queryOne = iContactPeopleService.list(query);
        if(queryOne==null || queryOne.size()==0) {
            peopleDto.setContactWechat(request.getWechatNumber());
            peopleDto.setCreateTime(new Date());
            peopleDto.setUpdateTime(new Date());
            iContactPeopleService.save(ContactPeopleConvert.INSTANCE.dtoToDo(peopleDto));
        }
        //客户新增请求去重
        List<UserBaseInfo> oldData = iUserBaseInfoService.list(new QueryWrapper<UserBaseInfo>()
                .eq("tel_phone", request.getTelPhone()));
        if(oldData!=null && oldData.size()>0) {
            return CommonResponse.builder().code(HttpStatus.HTTP_BAD_REQUEST).msg("无效请求,用户已存在").build();
        }
        if(request.getBelongParkId()==null || ParkEnum.getDescription(String.valueOf(request.getBelongParkId())) == null) {
            return CommonResponse.builder().code(HttpStatus.HTTP_OK).msg("无效请求，园区编号不存在").data(null).build();
        }
        request.setUserId(
                GeneratorCodeUtil.generateCode(ParkEnum.getDescription(String.valueOf(request.getBelongParkId()))));
        return CommonResponse.builder().code(HttpStatus.HTTP_OK).msg("操作成功").data(iUserBaseInfoService.addOne(UserBaseInfoConvert.INSTANCE.dtoToDo(request))).build();
    }

}
